extends Node
class_name DataSource




# 
const drama = "drama"
const npc_fight = "npc_fight"

const poke_item = "poke_item"


const dialig = "dialig"


const pick_item = "pick_item"
const pick_sprite = "pick_sprite"
const init_sprite = "init_sprite"
const appoint_sprite = "appoint_sprite"
#const pick_sprite_change = "pick_sprite_change"
const coin_consume = "coin_consume"
# 教授商店
const teach_shop = "teach_shop"
const sprite_change = "sprite_change"
const gyms = "gyms"


const map_area = "map_area"
const map_sprite_area = "map_sprite_area"
const map_sprite_area_item = "map_sprite_area_item"

const map_register = "map_register"
const map_trees = "map_trees"
const map_door = "map_door"
const map_prop = "map_prop"
const map_interactive = "map_interactive"
const map_center_recover = "map_center_recover"
const map_npc = "map_npc"
const map_decoration = "map_decoration"

const shop = "shop"
const shop_details = "shop_details"
const conditions = "conditions"
const series_file_path = "res://series/seriesData.json"

# 世界地图文件路径
const word_map_data_info_path = "res://map/data/word_map.json"


# 元数据定义
const data_meta = {
	"poke_item":{
		"name": "poke_item"
	},
	"dialig":{
		"name": "dialig",
	},
	"drama":{
		"name": "drama",
	},
	"npc_fight":{
		"name": "npc_fight",
	},
	"map_area":{
		"name": "map_area",
	},
	"map_sprite_area":{
		"name": "map_sprite_area",
	},
	"map_sprite_area_item":{
		"name": "map_sprite_area_item",
	},
	"map_register":{
		"name": "map_register",
	},
	"map_trees":{
		"name": "map_trees",
	},
	"map_door":{
		"name": "map_door",
	},
	"map_interactive":{
		"name": "map_interactive",
	},
	"map_prop":{
		"name": "map_prop",
	},
	"map_center_recover":{
		"name": "map_center_recover",
	},
	"map_npc":{
		"name": "map_npc",
	},
	"map_decoration":{
		"name": "map_decoration",
	},
	"shop":{
		"name": "shop",
	},
	"shop_details":{
		"name": "shop_details",
	},
	"pick_sprite":{
		"name": "pick_sprite"
	},
	"init_sprite":{
		"name": "init_sprite"
	},
	"sprite_change":{
		"name": "sprite_change"
	},
	"appoint_sprite":{
		"name": "appoint_sprite"
	},
	"coin_consume":{
		"name": "coin_consume"
	},
	"pick_item":{
		"name": "pick_item"
	},
	"gyms":{
		"name": "gyms"
	},
	"conditions":{
		"name": "conditions"
	},
	"teach_shop":{
		"name": "teach_shop"
	},
	
}

const data = {
}

const series_data = {
	"meta":{},
	"spirits": [],
	"items": [],
	"skills": [],
}


# 定义一个枚举类型  
enum assets_type {  
	item = 1  
	spirit = 2
}

func _ready():	
	init_data()

static func init_data():
	load_series_data()
	load_meta_data()
	



# 加载系列数据
static func load_series_data():
	var data = FileHelper.read(series_file_path)
	
	if data:
		for key in data.keys():
			series_data[key] = data[key]
		
	
# 加载地区数据
static func load_meta_data():
	for key in data_meta.keys():
		var meta = data_meta[key]
		data[meta["name"]] = []
		
		var path = get_meta_data_path(meta["name"])
		
		var list = FileHelper.read(path)

		
		if list: data[meta["name"]] = list[meta["name"]]
		

static func get_meta_data_path(name):
	return "res://data/%s.json" % name

static func get_serie_spirit_list():
	return series_data["spirits"]


static func get_spirit_by_id(id):
	for item in get_serie_spirit_list():
		if item["id"] == id:
			return item
	
	return null


static func get_skill_by_id(id):
	for item in get_serie_skill_list():
		if item["id"] == id:
			return item
	
	return null

static func get_serie_item_list():
	return series_data["items"]
	

static func get_item_by_id(id):
	for item in get_serie_item_list():
		if item["id"] == id:
			return item
	
	return null


static func get_serie_skill_list():
	return series_data["skills"]



#========================= 资源路径 ================
const icon_cache = {}


static func get_texture(path):
	if not icon_cache.get(path, null):
		if ResourceLoader.exists(path):
			icon_cache[path] = load(path)

	return icon_cache.get(path,null)


static func pad_with_zeros(num, length):  
	# 将数字转换为字符串  
	var num_str = str(num)  
	  
	# 如果字符串长度小于指定长度，则在前面添加0  
	while num_str.length() < length:  
		num_str = "0" + num_str  
	  
	# 返回结果字符串  
	return num_str 
	
# 获取图片
static func get_texture_by_type(type, id):
	var path = get_icon_path(type, id)
	return get_texture(path)


static func exist_texture_by_type(type, id):
	var path = get_icon_path(type, id)
	return FileHelper.file_exits(path)
	



# 获取数据的icon地址
static func get_icon_path(type, id):
	if type == 1:
		var path = str("res://assets/item/",id,".png")
		
		return path
	
	if type == 2:
		var no = pad_with_zeros(id, 3)
		return str("res://assets/sprite/icon/icon",no,"_c.png")




#=====================   dao区域   ==================
		
# = 通用方法

static func get_data_list(name, deep_copy=false):
	var arr = data[name]
	
	if deep_copy:
		var new_arr = []
		for i in arr:
			new_arr.append(i.duplicate(true))
		
		arr = new_arr
		
	return arr 
	
static func set_data_list(name, list:Array):
	data[name] = list
	
	# 保存到文件
	save_list(name)
	

# 保存数据
static func save_list(name):
	var meta = data_meta[name]
	var save_data = {}
	save_data[name] = data[name]
	
	var path = get_meta_data_path(meta["name"])
	
	FileHelper.save(path, save_data)


# 添加一条剧本数据
static func add_data(name, data):
	var list = get_data_list(name)
	
	# 保证必须有id
	if !data.get("id", null):
		data["id"] = get_list_max_id(list)+1
		
	list.append(data)
	
	# 保存到文件里
	save_list(name)

# 删除一条数据
static func delete_data_by_id(name, id):
	var index = 0 
	var data_list = get_data_list(name)
	for item in data_list:
		if item["id"] == id:
			data_list.remove(index)
			break
		
		index+=1
		
	# 保存到文件
	save_list(name)

# 删除数据
static func delete_data_by(name, val, by="id"):
	var index = 0 
	var data_list = get_data_list(name)
	var new_list = []

	for item in data_list:
		if item[by] != val:
			new_list.append(item)
			
		index+=1
	
	set_data_list(name, new_list)
	
	
# 编辑一条数据
static func update_data(name, data):
	for item in get_data_list(name):
		if item["id"] == data["id"]:
			# 更新数据
			for key in data.keys():
				item[key] = data[key]
	
	# 保存到文件里
	save_list(name)


# 获取list数据的索引
static func get_data_list_index(list, val, by="id"):
	
	for index in range(list.size()):
		if data[index][by] == val:
			return index
			
	return -1
	
# 获取单条数据
static func get_data_by_one(name, val, by="id"):
	for data in get_data_list(name):
		if data[by] == val:
			return data
			
	return null

# 获取多条数据
static func get_data_by_list(name, val, by="id"):
	var list = []
	for data in get_data_list(name):
		if data[by] == val:
			list.append(data)
			
	return list

# 获取数据里id最大的值
static func get_list_max_id(list):
	var max_id = 0
	for data in list:
		max_id = max(max_id, data["id"])
	
	return max_id


	

static func ids_to_str(list):
	var s = ""
	for i in range(list.size()):
		if i != 0:
			s += ","
		
		s += str(list[i])
	
	return s	

static func str_to_ids(s):
	if s:
		return s.split(",")
		
	return []	
